<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<style>
    .el-table .warning-row {
        background: oldlace;
    }

    .el-table .success-row {
        background: #f0f9eb;
    }
</style>
<body>
<!--注意了页面上面表格左边的序号不是每个Brand对象的id，它只是自动生成的序号-->
<div id="app">
<!--    搜索表单-->
    <el-form :inline="true" :model="brandsearch" class="demo-form-inline">
        <el-form-item label="当前状态">
            <el-select v-model="brandsearch.status" placeholder="当前状态">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="企业名称">
            <el-input v-model="brandsearch.companyName" placeholder="企业名称"></el-input>
        </el-form-item>
        <el-form-item label="品牌名称">
            <el-input v-model="brandsearch.brandName" placeholder="品牌名称"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
    </el-form>
<!--    按钮-->
    <el-row>
        <el-button type="danger" plain @click="deleteByIds">批量删除</el-button>
        <el-button type="primary" plain @click="dialogVisible = true">新增</el-button>
    </el-row>
<!--    新增数据的表单对话框-->
    <el-dialog
            title="新增品牌"
            :visible.sync="dialogVisible"
            width="30%"
            >
        <el-form ref="form" :model="brand" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="brand.brandName"></el-input>
            </el-form-item>
            <el-form-item label="企业名称">
                <el-input v-model="brand.companyName"></el-input>
            </el-form-item>
            <el-form-item label="排序">
                <el-input v-model="brand.ordered"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="brand.description"></el-input>
            </el-form-item>
            <el-form-item label="状态">
                <el-switch v-model="brand.status"
                           active-value="1"
                           inactive-value="0"></el-switch>
            </el-form-item>


            <el-form-item>
                <el-button type="primary" @click="addBrand">提交</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>
    <!--    修改数据的表单对话框-->
    <el-dialog
            title="修改品牌"
            :visible.sync="dialogVisible2"
            width="30%"
    >
        <el-form ref="form" :model="brandupdate" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="brandupdate.brandName"></el-input>
            </el-form-item>
            <el-form-item label="企业名称">
                <el-input v-model="brandupdate.companyName" ></el-input>
            </el-form-item>
            <el-form-item label="排序">
                <el-input v-model="brandupdate.ordered"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="brandupdate.description"></el-input>
            </el-form-item>
            <el-form-item label="状态">
                <el-switch v-model="brandupdate.status"
                           active-value="1"
                           inactive-value="0"></el-switch>
            </el-form-item>


            <el-form-item>
                <el-button type="primary" @click="updateBrand">提交</el-button>
                <el-button @click="dialogVisible2 = false">取消</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>
    <!-- 表格-->
    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    type="index"
                    width="50">
            </el-table-column>
            <el-table-column
                    prop="brandName"
                    label="品牌名称"
                    align="center"
                    >
            </el-table-column>
            <el-table-column
                    prop="companyName"
                    label="企业名称"
                    align="center"
                    >
            </el-table-column>
            <el-table-column
                    prop="ordered"
                    align="center"
                    label="排序">
            </el-table-column>
<!--            prop="status"-->
            <el-table-column
                    prop="statusStr"
                    align="center"
                    label="当前状态">
            </el-table-column>
<!--            <el-table-column-->
<!--                    align="center"-->
<!--                    label="操作">-->
<!--                <el-row>-->
<!--                    <el-button type="primary" @click="dialogVisible2 = true">修改</el-button>-->
<!--                    <el-button type="danger" @click="deleteBrand">删除</el-button>-->
<!--                </el-row>-->
<!--            </el-table-column>-->
            <el-table-column
                    align="center"
                    label="操作">
                <template slot-scope="scope">
                    <el-button type="primary"  @click="updatebtn(scope.$index)">修改</el-button>
                    <el-button type="danger"  @click="deleteBrand(scope.$index)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </template>

    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount">
    </el-pagination>


</div>




<script src="js/axios-0.18.0.js"></script>
<script src="js/vue.js"></script>
<script src="element-ui/lib/index.js"></script>
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
<script>

    new Vue({
        el:"#app",
        mounted(){
            // var _this=this;
            //
            // axios({
            //     method:"get",
            //     url:"http://localhost:8080/brand-case/brand2/selectAll"
            // }).then(function (resp){
            //     _this.tableData=resp.data;
            //     console.log(1)
            //     console.log(_this.tableData)
            // })
            this.selectAll();

        },
        data() {
            return {
                id_del:'',
                //总记录数
                totalCount:50,
                //被选中的ids数组，要被批量删除的id
                selectedIds:[],
                // 当前页码
                currentPage: 1,
                pageSize:5,

                //添加数据的对话框是否展示的标记
                dialogVisible: false,
                //修改数据的对话框是否展示的标记
                dialogVisible2: false,
                //品牌模型的数据
                brand: {
                    status: '',
                    brandName: '',
                    companyName: '',
                    id:'',
                    ordered:'',
                    description:''
                },
                //搜索时使用的品牌模型的数据
                brandsearch: {
                    status: '',
                    brandName: '',
                    companyName: '',
                    id:'',
                    ordered:'',
                    description:''
                },
                //修改时使用的品牌模型的数据
                brandupdate: {
                    status: '',
                    brandName: '',
                    companyName: '',
                    id:'',
                    ordered:'',
                    description:''
                },
                //修改时使用的品牌模型的数据
                branddata: {
                    status: '',
                    brandName: '',
                    companyName: '',
                    id:'',
                    ordered:'',
                    description:''
                },

                //复选框选中数据集合
                multipleSelection: [],
                //表格数据
                tableData: []
            }
        },
        methods: {
            // 复选框选中后执行的方法 （为批量删除服务）
            handleSelectionChange(val) {
                this.multipleSelection = val;

                console.log(this.multipleSelection);
            },
            // 批量删除
            deleteByIds(){

                this.$confirm('此操作将永久删除该数据, 是否继续?',  {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    for (let i = 0; i < this.multipleSelection.length; i++) {
                        let selectionElement=this.multipleSelection[i];
                        this.selectedIds[i]=selectionElement.id;
                    }
                    var _this=this;
                    axios({
                        method:"post",
                        url:"http://localhost:8080/brand-case/brand2/deleteByIds",
                        data:_this.selectedIds
                    }).then(function (resp){
                        if(resp.data=="success"){

                            _this.selectAll();

                            _this.$message({
                                message: '恭喜你，删除成功',
                                type: 'success'
                            });
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });


            },

            // 获取每页执行条数（为条件查询服务）
            handleSizeChange(val) {
                // console.log(`每页 ${val} 条`);
                this.pageSize=val;
                this.selectAll();
            },
            // 获取当前页码（为条件查询服务）
            handleCurrentChange(val) {
                //console.log(`当前页: ${val}`);
                this.currentPage=val;
                this.selectAll();
            },
            // 条件查询（包括了条件查询和分页查询）
            selectAll() {
                // var _this=this;
                //
                // axios({
                //     method:"get",
                //     url:"http://localhost:8080/brand-case/brand2/selectAll"
                // }).then(function (resp){
                //     _this.tableData=resp.data;
                //
                // })
                // var _this=this;
                //
                // axios({
                //     method:"get",
                //     url:"http://localhost:8080/brand-case/brand2/selectByPage?currentPage="+this.currentPage+"&pageSize="+this.pageSize
                // }).then(function (resp){
                //     _this.tableData=resp.data.rows;
                //     _this.totalCount=resp.data.totalCount;
                //
                //
                // })
                var _this=this;

                axios({
                    method:"post",
                    url:"http://localhost:8080/brand-case/brand2/selectByPageAndCondition?currentPage="+this.currentPage+"&pageSize="+this.pageSize,
                    data:this.brandsearch
                }).then(function (resp){
                    _this.tableData=resp.data.rows;
                    _this.totalCount=resp.data.totalCount;


                })

            },
            // 点击查询按钮后执行的方法
            onSubmit() {
                this.selectAll();
            },

            //element ui粘贴过来的，好像是用于展示页面用的
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },

            // 添加数据
            addBrand(){
                var _this=this;
                axios({
                    method:"post",
                    url:"http://localhost:8080/brand-case/brand2/add",
                    data:_this.brand
                }).then(function (resp){
                    if(resp.data=="success"){

                        _this.dialogVisible = false;

                        _this.selectAll();

                        _this.$message({
                            message: '恭喜你，添加成功',
                            type: 'success'
                        });
                    }
                })


            },
            // 删除数据
            deleteBrand(index){
                let brand=this.tableData[index];
                this.id_del=brand.id;

                console.log(this.id_del)
                this.$confirm('此操作将永久删除该数据, 是否继续?', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    var _this=this;
                    axios({
                        method: "post",
                        url: "http://localhost:8080/brand-case/brand2/delete",
                        data: this.id_del
                    }).then(function (resp){
                        if(resp.data=="success"){

                            _this.selectAll();

                            _this.$message({
                                message: '恭喜你，删除成功',
                                type: 'success'
                            });
                        }
                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });


            },
            // 修改数据
            updatebtn(id){
                this.dialogVisible2 = true;
                 //修改的表单框里自动填有原数据 改用v-bind就行
                // this.branddata=this.tableData[id];
                // this.brandupdate = this.branddata;

                //修改的表单框里没有默认数据
                this.branddata=this.tableData[id];
                this.brandupdate.id=this.branddata.id;


                //下面这个代码不推荐使用
                //this.brandupdate=this.tableData[id];
                // 这种做法是不行的，
                // 会使得表单里的数据一修改，然后页面上面的显示的数据也会被修改
                //虽然没有刷新一下页面就恢复原状了，但是就是不太好

            },
            updateBrand(){

                var _this=this;
                console.log(_this.brandupdate)

                axios({
                    method:"post",
                    url:"http://localhost:8080/brand-case/brand2/update",
                    data:_this.brandupdate
                }).then(function (resp){
                    if(resp.data=="success"){
                        console.log(resp.data)

                        _this.dialogVisible2 = false;

                        _this.selectAll();

                        _this.$message({
                            message: '恭喜你，修改成功',
                            type: 'success'
                        });
                    }
                })


            },

        }


    })


</script>
</body>
</html>